package com.beikbank.cloud.controller;


import com.beikbank.cloud.bean.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.annotation.SessionScope;

/**
 * Created by wangzhanjin on 2017/10/10.
 */
@RestController
@SessionScope
public class MovieController {
     @Autowired
     private RestTemplate restTemplate;

     @GetMapping("/movie/{id}")
     @HystrixCommand(fallbackMethod = "findByIdFallback",commandProperties = @HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE"))
     //参数返回值一致,配置了HystrixProperty后，两个线程是同一个线程
     public User findById(@PathVariable Long id){
          return this.restTemplate.getForObject("http://localhost:7900/simple/"+id, User.class);
     }

     public User findByIdFallback(Long id){
          User user = new User();
          user.setId(0L);
          return user;
     }
}
